package com.alinesno.infra.base.search.gateway.controller;

import cn.hutool.core.util.IdUtil;
import com.alinesno.infra.base.search.entity.DatasetClientEntity;
import com.alinesno.infra.base.search.entity.SearchLogEntity;
import com.alinesno.infra.base.search.service.IDatasetClientService;
import com.alinesno.infra.base.search.service.ISearchLogService;
import com.alinesno.infra.common.core.constants.SpringInstanceScope;
import com.alinesno.infra.common.extend.datasource.annotation.DataPermissionQuery;
import com.alinesno.infra.common.extend.datasource.annotation.DataPermissionSave;
import com.alinesno.infra.common.facade.datascope.PermissionQuery;
import com.alinesno.infra.common.facade.pageable.DatatablesPageBean;
import com.alinesno.infra.common.facade.pageable.TableDataInfo;
import com.alinesno.infra.common.facade.response.AjaxResult;
import com.alinesno.infra.common.web.adapter.login.account.CurrentAccountJwt;
import com.alinesno.infra.common.web.adapter.rest.BaseController;
import io.swagger.annotations.Api;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 处理与SearchLogEntity相关的请求的Controller。
 * 继承自BaseController类并实现ISearchLogService接口。
 *
 * @author luoxiaodong
 * @version 1.0.0
 */
@Slf4j
@Api(tags = "SearchLog")
@RestController
@Scope(SpringInstanceScope.PROTOTYPE)
@RequestMapping("/api/infra/base/search/datasetClient")
public class DatasetClientController extends BaseController<DatasetClientEntity, IDatasetClientService> {

    @Autowired
    private IDatasetClientService service;

    /**
     * 获取SearchLogEntity的DataTables数据。
     *
     * @param request HttpServletRequest对象。
     * @param model Model对象。
     * @param page DatatablesPageBean对象。
     * @return 包含DataTables数据的TableDataInfo对象。
     */
    @ResponseBody
    @PostMapping("/datatables")
    public TableDataInfo datatables(HttpServletRequest request, Model model, DatatablesPageBean page) {
        log.debug("page = {}", ToStringBuilder.reflectionToString(page));
        return this.toPage(model, this.getFeign(), page);
    }

    /**
     * 生成接口密钥
     */
    @DataPermissionQuery
    @PostMapping("/generateApiKey")
    public AjaxResult generateApiKey(PermissionQuery query) {

        Long currentUserId = CurrentAccountJwt.getUserId();

        String apiKey = service.generateApiKey(currentUserId , query);
        return AjaxResult.success("生成成功", apiKey);
    }

    /**
     * 更新密钥状态
     */
    @DataPermissionQuery
    @PostMapping("/updateApiKey")
    public AjaxResult updateApiKey(PermissionQuery query) {

        Long currentUserId = CurrentAccountJwt.getUserId();

        String newApiKey = service.updateKeyStatus(currentUserId , query);
        return AjaxResult.success("更新成功", newApiKey);
    }

    /**
     * 获取当前用户的API密钥
     * @return 当前用户的API密钥（未生成则返回空）
     */
    @GetMapping("/getCurrentApiKey")
    public AjaxResult getCurrentApiKey() {
        Long currentUserId = CurrentAccountJwt.getUserId();
        log.debug("获取当前用户[{}]的API密钥", currentUserId);

        String apiKey = service.getCurrentUserApiKey(currentUserId);
        return AjaxResult.success("操作成功" , apiKey);
    }

    @Override
    public IDatasetClientService getFeign() {
        return this.service;
    }
}